<template>
  <view>
    <!-- 是否显示弹窗-->
    <view>
      <view v-if="isMask">
        <popup
          type="5"
          :version="version"
          :content="desc"
          @close="closeMask()"
          @eventClick="update()"
        ></popup>
      </view>
    </view>
    <toast ref="toast"></toast>
  </view>
</template>

<script>
import popup from "@/components/popup/ge-popup.vue";
import { latestVersion } from "@/api/cockpit/cockpit.js";
import config from "@/config";
export default {
  data() {
    return {
      isMask: false, //是否显示弹窗
      desc: "",
      version: "",
      forceUp: "",
      wgtUrl: "",
      versionBig: false,
      pkgUrl: "",
      baseUrl: config.baseUrl,
    };
  },
  components: {
    popup,
  },
  created() {
    // #ifdef APP-PLUS
    this.downWgt();
    // #endif
  },
  methods: {
    downWgt() {
      plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
        let params = {
          appType: "",
          clientType: "DRIVER_APP",
          versionNo: wgtinfo.version,
        };
        let plateForm = uni.getSystemInfoSync().platform;
        if (plateForm == "android") {
          params.appType = "Android";
        } else {
          params.appType = "IOS";
        }
        latestVersion(params)
          .then((res) => {
            if (res.code == 200) {
              
              if (res.data) {
                this.isMask = true;
                this.forceUp = res.data.forceUpdate;
                this.version = res.data.version;
                this.desc = res.data.description;
                // // 这里简单判定下，不相等就是有更新。
                // var currentVersions = wgtinfo.version.split(".");
                // var resultVersions = this.version.split(".");
                if (res.data.name.indexOf(".apk") != -1) {
                  this.versionBig = true;
                } else {
                  this.versionBig = false;
                }
                this.pkgUrl =
                  this.baseUrl + "/system/oss/public/" + res.data.url;
                this.wgtUrl =
                  this.baseUrl + "/system/oss/public/" + res.data.url;
              }
            } else {
              this.$modal.msg(res.msg);
            }
          })
          .catch((err) => {
            this.$modal.msg(err);
          });
      });
    },
    update() {
      this.isMask = false;
      // 分整包更新和热更新
      if (this.versionBig) {
        this.updateApk();
      } else {
        this.updatewgt();
      }
    },
    // 整包更新
    updateApk() {
      
      uni.showModal({
        //提醒用户更新
        title: "更新提示",
        content: "发现最新版本，请速速更新！",
        success: (modal) => {
          if (modal.confirm) {
            if (plus.os.name == "Android") {
              plus.runtime.openURL(this.pkgUrl);
            } else {
              // console.log("ios去商店");
              // //app后面可以写上你们软件的拼音~ id可以在苹果商店中分享软件链接中有~
              // // id替换成你们自己应用的id
              // plus.runtimes.launchApplication({
              // 	action: 'https://itunes.apple.com/cn/app/*****/id******'
              // }, function(e) {
              // 	console.log('Open system default browser failed: ' + e.message);
              // });
            }
          }
        },
      });
    },
    // 部分更新
    updatewgt() {
      uni.showLoading({
        title: "更新中……",
      });
      uni.downloadFile({
        //执行下载
        url: this.wgtUrl,
        success: (downloadResult) => {
          //下载成功
          if (downloadResult.statusCode === 200) {
            uni.showModal({
              title: "",
              content: "更新成功，确定现在重启吗？",
              confirmText: "重启",
              confirmColor: "#EE8F57",
              success: function (res) {
                if (res.confirm) {
                  plus.runtime.install(
                    //安装
                    downloadResult.tempFilePath,
                    {
                      force: true,
                    },
                    function (e) {
                      plus.runtime.restart();
                    },
                    function (e) {
                      plus.nativeUI.alert("更新失败！");
                    }
                  );
                }
              },
            });
          }
        },
        complete: () => {
          uni.hideLoading();
        },
      });
    },
    // app检测更新关闭弹窗
    closeMask() {
      if (this.forceUp == "1") {
        this.$modal.msg("如不更新，则会导致部分功能无法使用");
        return;
      } else {
        this.isMask = false;
      }
    },
  },
};
</script>

<style></style>
